import store from './index'
import * as def from '../config/def'

const player = new QMplayer()
//音乐时间改变，更新时间戳
player.on('timeupdate', () => {
  store.commit(
    'updateCurrentTime', 
    parseInt(player.currentTime)
  )
  store.commit(
    'updateDuration',
    parseInt(player.duration)
  )
})
//是否继续播放
player.on('ended', () => {
  store.commit('playContinue')
})

export default {
  state: {
    // 当前播放的歌曲
    song: {
      name: def.DEFAULT_SONG_NAME
    },
    // 播放列表
    playList: [],
    // 播放中的歌曲索引
    index: 0,
    // 歌曲是否正在播放
    playing: false,
    // 时间戳
    currentTime: 0,
    duration: 0,
    // 播放模式，默认顺序播放
    playMode: def.SEQUENTIAL
  },
  mutations: {
    // 设置播放列表
    setPlayList(state, playList) {
      state.playList = playList.list
      state.index = playList.index
      state.song = state.playList[state.index]
    },
    // 开始播放
    play(state) {
      player.play(state.song.mid)
      state.playing = true
    },
    // 播放播放列表中索引值歌曲
    playIndex(state, index) {
      state.index = index
      state.song = state.playList[index]
      player.play(state.song.mid)
    },
    // 暂停播放
    pause(state) {
      player.pause()
      state.playing = false
    },
    updateCurrentTime(state, time) {
      state.currentTime = time
    },
    updateDuration(state, time) {
      state.duration = time
    },
    playFront(state) {
      state.index = (state.index - 1 + state.playList.length) % state.playList.length
      state.song = state.playList[state.index]
      player.play(state.song.mid)
    },
    playNext(state) {
      state.index = (state.index + 1) % state.playList.length
      state.song = state.playList[state.index]
      player.play(state.song.mid)
    },
    playContinue(state) {
      switch (state.playMode) {
        case def.SINGLE: 
          break
        case def.SEQUENTIAL:
          state.index = (state.index + 1) % state.playList.length
          state.song = state.playList[state.index]
          break
        case def.RANDOM:
          state.index = Math.floor(Math.random() * state.playList.length)
          state.song = state.playList[state.index]
          break
      }
      player.play(state.song.mid)
    },
    changePlayMode(state) {
      state.playMode = (state.playMode + 1) % 3
    },
    addToPlayListAsNextPlay(state, item) {
      state.playList.splice(state.index + 1, 0, item)
    },
    addToPlayList(state, item) {
      state.playList.push(item)
    },
    deleteFromPlayList(state, index) {
      if(isNaN(index) || index >= state.playList.length)
        return false
      
      if(state.index === 0 && state.playList.length === 1){
        player.pause()
        state.song = {
          name: def.DEFAULT_SONG_NAME,
          singer: ''
        }
        state.coverImgUrl = def.DEFAULT_IMG
      } else if(index < state.index) {
        state.index --
      } else if(index === state.index) {
        state.song = state.playList[state.index + 1]
        player.play(state.song.mid)
      }
      state.playList.splice(index, 1)
    }
  },
  getters: {
    coverImgUrl: state => {
      if(typeof state.song.albummid === 'undefined') {
        return def.DEFAULT_IMG
      }else {
        return `https://y.gtimg.cn/music/photo_new/T002R500x500M000${state.song.albummid}.jpg`
      }
    },
    currentTime: state => 
      parseInt(state.currentTime / 60) + ':' 
      + (Array(2).join(0) + (state.currentTime % 60))
      .slice(-2),
    duration: state =>
      parseInt(state.duration / 60) + ':'
      + (Array(2).join(0) + (state.duration % 60))
      .slice(-2)
  }
}